// import { getMenuList } from '@/api/index';
// import lazyLoading from './lazyLoading.js';
import router from '@/router/index';
import { Message } from 'element-ui';
import Cookies from "js-cookie";
import Global from './global';
import * as Api from '@/api'

let util = {};

/* 根据环境获取域名 */
util.getBaseUrl = function() {
    let key = process.env.VUE_APP_TITLE;

    switch (key) {
        // 测试环境
        case 'test':
            baseUrl = "http://47.115.5.236:8505";
            break
            // 正式环境
        case 'production':
            baseUrl = "";
            break
        default:
            baseUrl = "http://47.115.5.236:8505";
    }
    return baseUrl;
};

util.title = function(title) {
    title = title || '互联网医院';
    window.document.title = title;
};

// util.inOf = function (arr, targetArr) {
//     let res = true;
//     arr.forEach(item => {
//         if (targetArr.indexOf(item) < 0) {
//             res = false;
//         }
//     });
//     return res;
// };

util.oneOf = function(ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.getRouterObjByName = function(routers, name) {
    if (!name || !routers || !routers.length) {
        return null;
    }
    // debugger;
    let routerObj = null;
    for (let item of routers) {
        if (item.name == name) {
            return item;
        }
        routerObj = util.getRouterObjByName(item.children, name);
        if (routerObj) {
            return routerObj;
        }
    }
    return null;
};

util.handleTitle = function(vm, item) {
    // if (typeof item.title == 'object') {
    //     return vm.$t(item.title.i18n);
    // } else {
    return item.title;
    // }
};

util.setCurrentPath = function(vm, name) {
    let title = '互联网医院';
    let isOtherRouter = false;
    vm.$store.state.app.routers.forEach(item => {
        if (item.children.length == 1) {
            if (item.children[0].name == name) {
                title = util.handleTitle(vm, item);
                if (item.name == 'otherRouter') {
                    isOtherRouter = true;
                }
            }
        } else {
            item.children.forEach(child => {
                if (child.name == name) {
                    title = util.handleTitle(vm, child);
                    if (item.name == 'otherRouter') {
                        isOtherRouter = true;
                    }
                }
            });
        }
    });
    let currentPathArr = [];
    if (name == 'home_index') {
        currentPathArr = [{
            title: util.handleTitle(vm, util.getRouterObjByName(vm.$store.state.app.routers, 'home_index')),
            path: '',
            name: 'home_index'
        }];
    } else if ((name.indexOf('_index') >= 0 || isOtherRouter) && name !== 'home_index') {
        currentPathArr = [{
                title: util.handleTitle(vm, util.getRouterObjByName(vm.$store.state.app.routers, 'home_index')),
                path: '/home',
                name: 'home_index'
            },
            {
                title: title,
                path: '',
                name: name
            }
        ];
    } else {
        let currentPathObj = vm.$store.state.app.routers.filter(item => {
            if (item.children.length <= 1) {
                return item.children[0].name == name;
            } else {
                let i = 0;
                let childArr = item.children;
                let len = childArr.length;
                while (i < len) {
                    if (childArr[i].name == name) {
                        return true;
                    }
                    i++;
                }
                return false;
            }
        })[0];
        if (currentPathObj.children.length <= 1 && currentPathObj.name == 'home') {
            currentPathArr = [{
                title: '首页',
                path: '',
                name: 'home_index'
            }];
        } else if (currentPathObj.children.length <= 1 && currentPathObj.name !== 'home') {
            currentPathArr = [{
                    title: '首页',
                    path: '/home',
                    name: 'home_index'
                },
                {
                    title: currentPathObj.title,
                    path: '',
                    name: name
                }
            ];
        } else {
            let childObj = currentPathObj.children.filter((child) => {
                return child.name == name;
            })[0];
            currentPathArr = [{
                    title: '首页',
                    path: '/home',
                    name: 'home_index'
                },
                {
                    title: currentPathObj.title,
                    path: '',
                    name: currentPathObj.name
                },
                {
                    title: childObj.title,
                    path: currentPathObj.path + '/' + childObj.path,
                    name: name
                }
            ];
        }
    }
    vm.$store.commit('setCurrentPath', currentPathArr);

    return currentPathArr;
};

util.openNewPage = function(vm, name, argu, query) {
    if (!vm.$store) {
        return;
    }
    let pageOpenedList = vm.$store.state.app.pageOpenedList;
    let openedPageLen = pageOpenedList.length;
    let i = 0;
    let tagHasOpened = false;
    while (i < openedPageLen) {
        if (name == pageOpenedList[i].name) { // 页面已经打开
            vm.$store.commit('pageOpenedList', {
                index: i,
                argu: argu,
                query: query
            });
            tagHasOpened = true;
            break;
        }
        i++;
    }
    if (!tagHasOpened) {
        let tag = vm.$store.state.app.tagsList.filter((item) => {
            if (item.children) {
                return name == item.children[0].name;
            } else {
                return name == item.name;
            }
        });
        tag = tag[0];
        if (tag) {
            tag = tag.children ? tag.children[0] : tag;
            if (argu) {
                tag.argu = argu;
            }
            if (query) {
                tag.query = query;
            }
            vm.$store.commit('increateTag', tag);
        }
    }
    vm.$store.commit('setCurrentPageName', name);
};

util.toDefaultPage = function(routers, name, route, next) {
    let len = routers.length;
    let i = 0;
    let notHandle = true;
    while (i < len) {
        if (routers[i].name == name && routers[i].children && routers[i].redirect == undefined) {
            route.replace({
                name: routers[i].children[0].name
            });
            notHandle = false;
            next();
            break;
        }
        i++;
    }
    if (notHandle) {
        next();
    }
};

util.fullscreenEvent = function(vm) {
    vm.$store.commit('initCachepage');
};

util.initRouter = function(vm) {
    const constRoutes = [];
    const otherRoutes = [];

    // 404路由需要和动态路由一起加载
    const otherRouter = [{
        path: '/*',
        name: 'error-404',
        meta: {
            title: '404-页面不存在'
        },
        component: 'error-page/404'
    }];
    // 判断用户是否登录
    let userInfo = Cookies.get('userInfo')
    if (!userInfo) {
        // 未登录
        return;
    }
    // if (!vm.$store.state.app.added) {
    //     vm.$Loading.start();
    //     // 第一次加载 读取数据
    //     let accessToken = window.localStorage.getItem('webToken');
    //     // 加载菜单
    //     axios.get(getMenuList, { headers: { 'accessToken': accessToken } }).then(res => {
    //         vm.$Loading.finish();
    //         let menuData = res.result;
    //         if (!menuData) {
    //             return;
    //         }
    //         util.initAllMenuData(constRoutes, menuData);
    //         util.initRouterNode(otherRoutes, otherRouter);
    //         // 添加所有主界面路由
    //         vm.$store.commit('updateAppRouter', constRoutes.filter(item => item.children.length > 0));
    //         // 添加全局路由
    //         vm.$store.commit('updateDefaultRouter', otherRoutes);
    //         // 添加菜单路由
    //         util.initMenuData(vm, menuData);
    //         // 缓存数据 修改加载标识
    //         window.localStorage.setItem('menuData', JSON.stringify(menuData));
    //         vm.$store.commit('setAdded', true);
    //     });
    // } else {
    //     // 读取缓存数据
    //     let data = window.localStorage.getItem('menuData');
    //     if (!data) {
    //         vm.$store.commit('setAdded', false);
    //         return;
    //     }
    //     let menuData = JSON.parse(data);
    //     // 添加菜单路由
    //     util.initMenuData(vm, menuData);
    // }
};

// 添加所有顶部导航栏下的菜单路由
util.initAllMenuData = function(constRoutes, data) {

    let allMenuData = [];
    data.forEach(e => {
        if (e.type == -1) {
            e.children.forEach(item => {
                allMenuData.push(item);
            })
        }
    })
    util.initRouterNode(constRoutes, allMenuData);
}

// 生成菜单格式数据
util.initMenuData = function(vm, data) {
    const menuRoutes = [];
    let menuData = data;
    // 顶部菜单
    let navList = [];
    menuData.forEach(e => {
        let nav = {
            name: e.name,
            title: e.title,
            icon: e.icon,
            isMenu: e.isMenu,
            url: e.url,
            description: e.description
        }
        navList.push(nav);
    })
    if (navList.length < 1) {
        return;
    }
    // 存入vuex
    vm.$store.commit('setNavList', navList);
    let currNav = window.localStorage.getItem('currNav')
    if (currNav) {
        // 读取缓存title
        for (var item of navList) {
            if (item.name == currNav) {
                vm.$store.commit('setCurrNavTitle', item.title);
                break;
            }
        }
    } else {
        // 默认第一个
        currNav = navList[0].name;
        vm.$store.commit('setCurrNavTitle', navList[0].title);
    }
    vm.$store.commit('setCurrNav', currNav);
    for (var item of menuData) {
        if (item.name == currNav) {
            // 过滤
            menuData = item.children;
            break;
        }
    }
    util.initRouterNode(menuRoutes, menuData);
    // 刷新界面菜单
    vm.$store.commit('updateMenulist', menuRoutes.filter(item => item.children.length > 0));

    let tagsList = [];
    vm.$store.state.app.routers.map((item) => {
        if (item.children.length <= 1) {
            tagsList.push(item.children[0]);
        } else {
            tagsList.push(...item.children);
        }
    });
    vm.$store.commit('setTagsList', tagsList);
};

// 生成路由节点
util.initRouterNode = function(routers, data) {

    for (var item of data) {
        let menu = Object.assign({}, item);
        // menu.component = import(`@/views/${menu.component}.vue`);
        // menu.component = lazyLoading(menu.component);

        if (item.children && item.children.length > 0) {
            menu.children = [];
            util.initRouterNode(menu.children, item.children);
        }

        let meta = {};
        // 给页面添加权限、标题、第三方网页链接
        meta.permTypes = menu.permTypes ? menu.permTypes : null;
        meta.title = menu.title ? menu.title + " - 互联网医院" : null;
        meta.url = menu.url ? menu.url : null;
        menu.meta = meta;

        routers.push(menu);
    }
};

/**
 * $message 提示
 */
util.message = function(txt = '', type = 'error') {
    Message.closeAll();
    Message({
        type: type,
        message: txt
    })
}

/* 参数加密 */
util.encrypt = function(word, keyStr) {
    keyStr = keyStr ? keyStr : 'lzkjabcdefg123we';
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.toString();
};

/**
 * 参数解密 
 * @param word 需要加密的参数字符串
 * @param keyStr 指定加密字段
 */
util.decrypt = function(word, keyStr) {
    keyStr = keyStr ? keyStr : 'lzkjabcdefg123we';
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
};

util.cameraAndHeadset = function() {
    //检测电脑设备是否已经安装了摄像头
    console.log(navigator.mediaDevices, '检测电脑设备是否已经安装了摄像头')
    if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
        // Firefox 38+ seems having support of enumerateDevicesx
        navigator.enumerateDevices = function(callback) {
            navigator.mediaDevices.enumerateDevices().then(callback);
        };
    }
    var MediaDevices = [];
    var isHTTPs = location.protocol === 'https:';
    var canEnumerate = false;

    if (typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
        canEnumerate = true;
    } else if (navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
        canEnumerate = true;
    }

    var hasMicrophone = false;
    var hasSpeakers = false;
    var hasWebcam = false;

    var isMicrophoneAlreadyCaptured = false;
    var isWebcamAlreadyCaptured = false;

    function checkDeviceSupport(callback) {
        if (!canEnumerate) {
            return;
        }

        if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
            navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
        }

        if (!navigator.enumerateDevices && navigator.enumerateDevices) {
            navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
        }

        if (!navigator.enumerateDevices) {
            if (callback) {
                callback();
            }
            return;
        }

        MediaDevices = [];
        navigator.enumerateDevices(function(devices) {
            devices.forEach(function(_device) {
                var device = {};
                for (var d in _device) {
                    device[d] = _device[d];
                }

                if (device.kind === 'audio') {
                    device.kind = 'audioinput';
                }

                if (device.kind === 'video') {
                    device.kind = 'videoinput';
                }

                var skip;
                MediaDevices.forEach(function(d) {
                    if (d.id === device.id && d.kind === device.kind) {
                        skip = true;
                    }
                });

                if (skip) {
                    return;
                }

                if (!device.deviceId) {
                    device.deviceId = device.id;
                }

                if (!device.id) {
                    device.id = device.deviceId;
                }

                if (!device.label) {
                    device.label = 'Please invoke getUserMedia once.';
                    if (!isHTTPs) {
                        device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
                    }
                } else {
                    if (device.kind === 'videoinput' && !isWebcamAlreadyCaptured) {
                        isWebcamAlreadyCaptured = true;
                    }

                    if (device.kind === 'audioinput' && !isMicrophoneAlreadyCaptured) {
                        isMicrophoneAlreadyCaptured = true;
                    }
                }

                if (device.kind === 'audioinput') {
                    hasMicrophone = true;
                }

                if (device.kind === 'audiooutput') {
                    hasSpeakers = true;
                }

                if (device.kind === 'videoinput') {
                    hasWebcam = true;
                }

                // there is no 'videoouput' in the spec.

                MediaDevices.push(device);
            });

            if (callback) {
                callback();
            }
        });
    }
    //end

    checkDeviceSupport(function() {
        if (hasWebcam == false) {
            console.log("没有摄像头！");
            return false;
        } else {
            if (isWebcamAlreadyCaptured == false) {
                console.log("捕获摄像头失败，请重新安装摄像头！");
                return false;
            }
        }
        if (hasMicrophone == false) {
            console.log("没有麦克风！");
            return false;
        } else {
            if (isMicrophoneAlreadyCaptured == false) {
                console.log("捕获麦克风失败，请重新安装麦克风！");
                return false;
            }
        }
    });
};


/* --- 根据id过滤 --- */
util.filterByIds = function(id, arr = []) {
    let temp = arr.find(o => {
        return o.id == id
    })

    return temp && temp.name || '';
}

/* --- 获取CA模式 --- */
util.isSilent = function(cb) {
    return new Promise((resolve) => {
        Api.getCaMode().then(res => {
            resolve(res.data.caMode && res.data.caMode == 2) // 1=非静默签，2=静默签
        }).catch(err => {
            resolve(false)
        });
    })

}
util.notSilentCa = async function() {
    let flag = await util.isSilent();  // flag true=静默签，false=非静默签
    return Global.isCa && !flag;
}

export default util;